/**
 * Created by gongxun on 16/12/14.
 */

var WhmjControlLayer = GameCCSBase.extend({

    Panel_control : null,
    Panel_chi : null,
    mNewActionList : null,

    Panel_selectGang : null,
    Image_selectGangs : null,
    mGangControls : null,

    Button_guo : null,
    Button_hu : null,
    Button_gang : null,
    Button_peng : null,
    Button_chi : null,


    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/WhmjControl.json");
        mo.gameHelper.fixIosText(this.ccsNode);

        //吃碰杠胡过
        this.Panel_control = mo.seekWidgetByName(this.ccsNode, "Panel_control");

        this.Button_guo = mo.seekWidgetByName(this.Panel_control, "Button_guo");
        this.Button_hu = mo.seekWidgetByName(this.Panel_control, "Button_hu");
        this.Button_gang = mo.seekWidgetByName(this.Panel_control, "Button_gang");
        this.Button_peng = mo.seekWidgetByName(this.Panel_control, "Button_peng");
        this.Button_chi = mo.seekWidgetByName(this.Panel_control, "Button_chi");
        this.Panel_selectGang = mo.seekWidgetByName(this.ccsNode, "Panel_selectGang");
        this.Panel_selectGang.setVisible(false);
        this.Panel_chi = mo.seekWidgetByName(this.ccsNode, "Panel_chi");
        this.Panel_chi.setVisible(false);

        this.loadUI();
        this.setVisible(false);
    },

    onEnter: function () {
        this._super();
        // this.reset();
    },

    loadUI: function () {
        //guo
        this.Button_guo.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.sendOpenControl([9]);
            }

        }, this);

        //hu
        this.Button_hu.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.sendOpenControl([3]);
            }
        }, this);

        //gang
        this.Button_gang.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.sendOpenControlGang();
            }
        }, this);

        //peng
        this.Button_peng.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.sendOpenControl([2]);
            }
        }, this);

        //chi
        this.Button_chi.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.showEatGroups();
            }
        }, this);

        //杠的多种情况
        this.Image_selectGangs = [];
        for (var i = 0; i < 4; i++){
            var card = new WhmjCard();
            card.setName("card");

            var Image_selectGang = mo.seekWidgetByName(this.Panel_selectGang, "Image_selectGang" + i);
            Image_selectGang.addChild(card);
            Image_selectGang.setTag(i);
            Image_selectGang.addTouchEventListener(function(sender, type){
                if (mo.gameHelper.getIsTouchEnd(type)) {
                    var index = sender.getTag();
                    var actionRequestwhmj = {};
                    actionRequestwhmj.code = this.mGangControls[index].code;
                    actionRequestwhmj.Cards1 = [this.mGangControls[index].card];
                    mo.whmjTableStatus.sendOpenCard(actionRequestwhmj);
                    cc.log("操作 code:" + actionRequestwhmj.code);
                }
            }, this);

            this.Image_selectGangs.push(Image_selectGang);
        }

        this.Panel_selectGang.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.Panel_selectGang.setVisible(false);
            }
        }, this);


    },

    //设置是否显示自己的操作
    showOperating : function(isShow, newActionList){
        this.setVisible(isShow);
        this.mNewActionList = newActionList;
        if (isShow){
            this.Panel_control.setVisible(true);

            var startPx = 860;
            startPx = this.setOperatingButton(this.Button_guo, [9], startPx);
            startPx = this.setOperatingButton(this.Button_hu, [3], startPx);
            startPx = this.setOperatingButton(this.Button_chi, [1], startPx);
            startPx = this.setOperatingButton(this.Button_gang, [4, 5, 6], startPx);
            startPx = this.setOperatingButton(this.Button_peng, [2], startPx);
        }
    },


    setOperatingButton : function(button, codes, pox){
        var isShow = false;
        for (var i = 0; i < codes.length; i++){
            for(var j = 0; j < this.mNewActionList.length; j++){
                var code = this.mNewActionList[j].code;
                if (code == codes[i]){
                    isShow = true;
                    if (code == 0){
                        if (this.mNewActionList[j].Cg == null){
                            isShow = false;
                            continue;
                        }
                    }
                    break;
                }
            }
        }

        if (isShow){
            button.setVisible(true);
            button.setPositionX(pox);

            pox -= 130;
        }else{
            button.setVisible(false);
        }
        return pox;
    },


    showEatGroups : function () {
        if(this.Panel_chi.isVisible()){
            return;
        }

        var eatGroups = mo.whmjTableStatus.getEatGroups();
        var eatGroupsLength = eatGroups.length;

        if (eatGroupsLength == 1){
            this.sendOpenControlChi(eatGroups[0]);
        } else if (eatGroupsLength > 1){
            this.Panel_chi.setVisible(true);
            this.Panel_chi.setPosition(cc.p(this.Button_chi.getPositionX(), 282));

            for (var i = 0; i < 3; i++){
                var Image_chi = mo.seekWidgetByName(this.Panel_chi, "Image_chi_" + i);
                Image_chi.setTag(i);
                Image_chi.setTouchEnabled(true);
                Image_chi.removeAllChildren();

                Image_chi.addTouchEventListener(function(sender, type){
                    if (mo.gameHelper.getIsTouchEnd(type)) {
                        var tag = sender.getTag();
                        this.sendOpenControlChi(eatGroups[tag]);
                    }
                }, this);


                var pos = - i * 100;
                Image_chi.setPositionX(pos);

                if(i < eatGroupsLength){
                    Image_chi.setVisible(true);


                    var cardSize = mo.whmjCardHelper.getDealCardSize();

                    var card1 = new WhmjCard();
                    card1.setCardNum(eatGroups[i][0]);
                    card1.setCardKind(0, 2);

                    card1.setPosition(cc.p(Image_chi.getContentSize().width/4 - cardSize.width/2, 0));
                    Image_chi.addChild(card1);

                    var card2 = new WhmjCard();
                    card2.setCardNum(eatGroups[i][1]);
                    card2.setCardKind(0, 2);
                    card2.setPosition(cc.p(Image_chi.getContentSize().width/4 * 3 - cardSize.width/2, 0));
                    Image_chi.addChild(card2);

                }else{
                    Image_chi.setTouchEnabled(false);
                    Image_chi.setVisible(false);
                }
            }
        }
    },

    //发送杠操作
    sendOpenControlGang : function(){
        var codes = [4, 5, 6];

        this.mGangControls = [];
        for(var j = 0; j < this.mNewActionList.length; j++){
            var code = this.mNewActionList[j].code;
            if (code == 4){
                //明杠直接打出
                mo.whmjTableStatus.sendOpenCard(this.mNewActionList[j]);
            }else if (code == 5 || code == 6){
                //暗杠和续杠让用户选择
                for (var i = 0; i < this.mNewActionList[j].Cards1.length; i++){
                    var gangJiegou = {};
                    gangJiegou.code = code;
                    gangJiegou.card = this.mNewActionList[j].Cards1[i];
                    this.mGangControls.push(gangJiegou);
                }
            }
        }

        if (this.mGangControls.length == 1){
            var actionRequestwhmj = {};
            actionRequestwhmj.code = this.mGangControls[0].code;
            actionRequestwhmj.Cards1 = [this.mGangControls[0].card];
            mo.whmjTableStatus.sendOpenCard(actionRequestwhmj);
            cc.log("操作 code:" + actionRequestwhmj.code);
        }else{
            this.Panel_selectGang.setVisible(true);
            for(var i = 0; i < 4; i++){
                var isShow = i < this.mGangControls.length;
                this.Image_selectGangs[i].setVisible(isShow);
                if (isShow){
                    var cardSize = cc.size(75 * 0.9, 111 * 0.9);
                    var bgSize = this.Image_selectGangs[i].getContentSize();
                    var p = cc.p(bgSize.width / 2 - cardSize.width / 2, bgSize.height / 2 - cardSize.height / 2)

                    var card = mo.seekWidgetByName(this.Image_selectGangs[i], "card");
                    card.setCardNum(this.mGangControls[i].card);
                    card.setCardKind(0, 3);
                    card.setScale(0.9);
                    card.setPosition(p);
                }
            }
        }
    },

    //发送吃操作
    sendOpenControlChi : function(eatGroup) {
        cc.log("发送吃的操作，eatGroup = " + eatGroup);

        this.Panel_chi.setVisible(false);
        this.setVisible(false);

        for(var j = 0; j < this.mNewActionList.length; j++){
            var code = this.mNewActionList[j].code;
            if (code == 1){
                this.mNewActionList[j].Cards1 = eatGroup;
                mo.whmjTableStatus.sendOpenCard(this.mNewActionList[j]);
                break;
            }
        }
    },

    //发送操作
    sendOpenControl : function(codes){
        for (var i = 0; i < codes.length; i++){
            for(var j = 0; j < this.mNewActionList.length; j++){
                var code = this.mNewActionList[j].code;
                if (code == codes[i]){
                    mo.whmjTableStatus.sendOpenCard(this.mNewActionList[j]);
                    cc.log("操作 code:" + code);
                    break;
                }
            }
        }
    },
});